//
//  SunRecordVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/31.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunRecordVC.h"
#import "SunRecordCell.h"
#import "MJRefresh.h"

@interface SunRecordVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    NSMutableDictionary *_dataArr;
    NSMutableArray *_daysArr;
    UIView *_blank;
    int _page;
}
@end

@implementation SunRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.type == 1){
        self.title = @"提现记录";
    }else {
        self.title = @"充值记录";
    }
    _page = 1;
    _dataArr = [NSMutableDictionary dictionary];
    _daysArr = [NSMutableArray array];
    [self creatUI];
    [[Sun shareSun]showActivity:self];
    [self requestToData];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
- (BOOL)navigationShouldPopOnBackButton{
    [self.navigationController popToRootViewControllerAnimated:YES];
    return false;
}

- (void)creatUI{
    _tb = [[UITableView alloc]init];
    [self.view addSubview:_tb];
    _tb.delegate = self;
    _tb.dataSource = self;
    [_tb lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    _tb.backgroundColor = WHITE;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.allowsSelection = NO;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void)refresh{
    _page = 1;
    [_dataArr removeAllObjects];
    [_daysArr removeAllObjects];
    [self requestToData];
}

- (void)loadMore{
    _page ++;
    [self requestToData];
}

- (void)creatBlank{
    _blank = [[UIView alloc]init];
    [self.view addSubview:_blank];
    [_blank lt_All:SunEdgesMake(Y(46), 0, 0, 0) save:YES];
    _blank.backgroundColor = WHITE;
    UIImageView *img = [[UIImageView alloc]init];
    [_blank addSubview:img];
    [img lt_XcenterTo:nil save:YES];
    img.image = [UIImage imageNamed:@"third_order_blank"];
    UILabel *label = [[UILabel alloc]init];
    [_blank addSubview:label];
    [label lt_XcenterTo:nil save:YES];
    [label lt_setTop:Y(18) to:img save:YES];
    [label lt_layoutView:label side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(20) save:YES];
    label.textColor = GRAY;
    label.font = [UIFont systemFontOfSize:19];
    label.text = @"暂无相关记录！";
}

- (void)requestToData{
    NSString *str = @"";
    if (self.type == 1) {
        str = @"cash";
    }else {
        str = @"recharge";
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_RECORD];
    [HttpRequest postURL:url withParam:@{@"type":str,@"page":[NSString stringWithFormat:@"%d",_page]} success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        NSDictionary *dic = requestDic[@"list"];
        if (_page == 1) {
            [_dataArr removeAllObjects];
        }
        if (dic.count > 0) {
            [_dataArr addEntriesFromDictionary:requestDic[@"list"]];
            [_tb.mj_footer resetNoMoreData];
            [_daysArr removeAllObjects];
            [_daysArr addObjectsFromArray:[_dataArr allKeys]];
            NSArray *sortArr = [_daysArr sortedArrayUsingSelector:@selector(compare:)];
            NSEnumerator *enumerator = [sortArr reverseObjectEnumerator];
            _daysArr =[[NSMutableArray alloc]initWithArray: [enumerator allObjects]];
            [_tb reloadData];
        }else{
            [_tb.mj_footer endRefreshingWithNoMoreData];
        }

        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
    }];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [_dataArr objectForKey:_daysArr[section]];
    return array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SunRecordCell *cell = [SunRecordCell cellWithTableView:tableView type:self.type];
    NSArray *array = [_dataArr objectForKey:_daysArr[indexPath.section]];
    [cell setCellData:array[indexPath.row] type:self.type];
    NSString *timeString = _daysArr[indexPath.section];
    if (indexPath.row == 0) {
        cell.dateLable.hidden = NO;
        cell.monthLabel.hidden = NO;
        cell.dateLable.text = [timeString substringFromIndex:[timeString length]-2];
        cell.monthLabel.text = [NSString stringWithFormat:@"%@月",[[timeString substringFromIndex:[timeString length]-5] substringToIndex:2]];
    }else{
        cell.dateLable.hidden = YES;
        cell.monthLabel.hidden = YES;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 58;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
